Add new lint manual_is_power_of_two#13327
Add new lint manual_is_power_of_two#13327bors merged 4 commits intorust-lang:masterfrom Sour1emon:master
manual_is_power_of_two#13327Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
|
|
☔ The latest upstream changes (presumably #13247) made this pull request unmergeable. Please resolve the merge conflicts. |
| && let ExprKind::Lit(lit1) = right.kind | ||
| && let LitKind::Int(Pu128(0), _) = lit1.node | ||
| { | ||
| let snippet = snippet_with_applicability(cx, left1.span, "..", &mut applicability); |
There was a problem hiding this comment.
Might make sense combine sugg and span_lint_and_sugg here and 48-58 so change in one place if needed
|
|
||
| let b = 4_i64; | ||
|
|
||
| // is_power_of_two only works for unsigned integers |
There was a problem hiding this comment.
Might be good idea mentioned about this at lint doc as well
llogiq
left a comment
There was a problem hiding this comment.
There is some duplication and the binary operations might be reversed. Either you implement the tests for both directions or you document the missing cases in the lint docs under # Known Problems and we can open an issue to amend the lint later. Both are fine.
|
☔ The latest upstream changes (presumably #12987) made this pull request unmergeable. Please resolve the merge conflicts. |
|
There are merge commits (commits with multiple parents) in your changes. We have a no merge policy so these commits will need to be removed for this pull request to be merged. You can start a rebase with the following commands: The following commits are merge commits: |
Co-authored-by: Ruby Lazuli <general@patchmixolydic.com>
Co-authored-by: Ruby Lazuli <general@patchmixolydic.com>
| if let ExprKind::MethodCall(method_name, reciever, _, _) = left.kind | ||
| && method_name.ident.as_str() == "count_ones" | ||
| && let &Uint(_) = cx.typeck_results().expr_ty(reciever).kind() | ||
| && check_lit(right, 1) | ||
| { | ||
| build_sugg(cx, expr, reciever, &mut applicability); | ||
| } | ||
|
|
||
| // 1 == a.count_ones() | ||
| if let ExprKind::MethodCall(method_name, reciever, _, _) = right.kind | ||
| && method_name.ident.as_str() == "count_ones" | ||
| && let &Uint(_) = cx.typeck_results().expr_ty(reciever).kind() | ||
| && check_lit(left, 1) | ||
| { | ||
| build_sugg(cx, expr, reciever, &mut applicability); | ||
| } |
There was a problem hiding this comment.
You can loop over [(left, right), (right, left)] to reduce the code duplication here and elsewhere.
|
Apart from a good deal of code duplication, this seems good to merge, we can refactor in a followup PR later. @bors r+ |
|
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Suggest using
is_power_of_two()instead of the manual implementations for some basic casesPart of #12894
changelog: new [
manual_is_power_of_two] lint